package com.sicau.web.controller.system;

import java.io.IOException;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.sicau.common.core.domain.entity.SysRole;
import com.sicau.common.core.domain.entity.SysUser;
import com.sicau.common.core.domain.model.LoginUser;
import com.sicau.common.utils.SecurityUtils;
import com.sicau.common.utils.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import com.sicau.common.annotation.Log;
import com.sicau.common.core.controller.BaseController;
import com.sicau.common.core.domain.AjaxResult;
import com.sicau.common.enums.BusinessType;
import com.sicau.system.domain.BTestResult;
import com.sicau.system.service.IBTestResultService;
import com.sicau.common.utils.poi.ExcelUtil;
import com.sicau.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 体测成绩Controller
 *
 * @author ruoyi
 * @date 2022-03-19
 */
@RestController
@RequestMapping("/system/result")
public class BTestResultController extends BaseController {
    @Autowired
    private IBTestResultService bTestResultService;

    /**
     * 查询体测成绩列表
     */
    @PreAuthorize("@ss.hasPermi('system:result:list')")
    @GetMapping("/list")
    public TableDataInfo list(BTestResult bTestResult) {
        startPage();
        if (hasRole("student")) {
            bTestResult.setUserName(SecurityUtils.getUsername());
        }
        List<BTestResult> list = bTestResultService.selectBTestResultList(bTestResult);
        return getDataTable(list);
    }

    /**
     * 导出体测成绩列表
     */
    @PreAuthorize("@ss.hasPermi('system:result:export')")
    @Log(title = "体测成绩", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BTestResult bTestResult) {
        List<BTestResult> list = bTestResultService.selectBTestResultList(bTestResult);
        ExcelUtil<BTestResult> util = new ExcelUtil<BTestResult>(BTestResult.class);
        util.exportExcel(response, list, "体测成绩数据");
    }

    /**
     * 导出体测成绩列表
     */
    @PreAuthorize("@ss.hasPermi('system:result:import')")
    @Log(title = "体测成绩", businessType = BusinessType.IMPORT)
    @PostMapping("/import")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<BTestResult> util = new ExcelUtil<BTestResult>(BTestResult.class);
        List<BTestResult> list = util.importExcel(file.getInputStream());
        return AjaxResult.success(bTestResultService.importResult(list));
    }

    /**
     * 获取体测成绩详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:result:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(bTestResultService.selectBTestResultById(id));
    }

    /**
     * 新增体测成绩
     */
    @PreAuthorize("@ss.hasPermi('system:result:add')")
    @Log(title = "体测成绩", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BTestResult bTestResult) {
        bTestResult.setCreateBy(SecurityUtils.getUsername());
        return toAjax(bTestResultService.insertBTestResult(bTestResult));
    }

    /**
     * 修改体测成绩
     */
    @PreAuthorize("@ss.hasPermi('system:result:edit')")
    @Log(title = "体测成绩", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BTestResult bTestResult) {
        return toAjax(bTestResultService.updateBTestResult(bTestResult));
    }

    /**
     * 删除体测成绩
     */
    @PreAuthorize("@ss.hasPermi('system:result:remove')")
    @Log(title = "体测成绩", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(bTestResultService.deleteBTestResultByIds(ids));
    }

    @GetMapping("/student/student")
    public AjaxResult selectStudents(@RequestParam(value = "name", required = false) String name) {
        return AjaxResult.success(bTestResultService.selectStudents(name));
    }

    /**
     * 判断用户是否拥有某个角色
     *
     * @param role 角色字符串
     * @return 用户是否具备某角色
     */
    private boolean hasRole(String role)
    {
        if (StringUtils.isEmpty(role))
        {
            return false;
        }
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (StringUtils.isNull(loginUser) || CollectionUtils.isEmpty(loginUser.getUser().getRoles()))
        {
            return false;
        }
        for (SysRole sysRole : loginUser.getUser().getRoles())
        {
            String roleKey = sysRole.getRoleKey();
            if (roleKey.equals(StringUtils.trim(role)))
            {
                return true;
            }
        }
        return false;
    }

}
